package com.jf.blog.controller;

import com.jf.blog.form.comment.CommentForm;
import com.jf.blog.service.ApiService;
import com.jf.blog.util.LoginUtil;
import com.jf.blog.util.ResultJson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
@Api(tags = "前台页面模块")
public class ApiController {
    @Autowired
    private ApiService apiService;

//    @GetMapping("/path/{path}")
//    @ApiOperation("验证地址")
//    public ResultJson path(@PathVariable String path) {
//        Userinfo userinfo = apiService.checkUsernameOrHref(path);
//        return userinfo == null ? ResultJson.error(404, "地址错误") : ResultJson.success();
//    }

    @GetMapping("/blog/list/{page}")
    @ApiOperation("首页博客列表分页")
    public ResultJson blogList(@PathVariable int page) {
        return apiService.getBlogPage(page);
    }

    @GetMapping("/info")
    @ApiOperation("博主信息")
    public ResultJson info() {
        return apiService.getVipInfo();
    }

    @GetMapping("/blog/{blogId}")
    @ApiOperation("博客详情")
    public ResultJson blogDetail(@PathVariable int blogId) {
        return apiService.blogDetail(blogId);
    }

    @GetMapping("/blog/{blogId}/comment/{page}")
    @ApiOperation("博客评论分页列表")
    public ResultJson blogCommentList(@PathVariable int blogId, @PathVariable int page) {
        return apiService.getCommentPage(page, blogId);
    }

    @PutMapping("/blog/comment/save")
    @ApiOperation("发表评论")
    public ResultJson saveComment(@RequestBody CommentForm form) {
        apiService.saveComment(form);
        return ResultJson.success();
    }

    @GetMapping("/category/list")
    @ApiOperation("分类列表")
    public ResultJson categoryList() {
        return apiService.getCategoryList();
    }

    @GetMapping("/blog/category/list")
    @ApiOperation("按照分类查看博客")
    public ResultJson blogListByCategoryId(int page, Integer categoryId) {
        return apiService.getBlogByCategoryId(page, categoryId);
    }

    @GetMapping("/tag/list")
    @ApiOperation("标签列表")
    public ResultJson tagList() {
        return apiService.getTagList();
    }

    @GetMapping("/blog/tag/list")
    @ApiOperation("按照标签查看博客")
    public ResultJson blogListByTagId(int page, Integer tagId) {
        return apiService.getBlogByTagId(page, tagId);
    }

    @PostMapping("/blog/read/count/{blogId}")
    @ApiOperation("更新阅读量")
    public ResultJson blogReadCount(@PathVariable int blogId) {
        apiService.updateReadCount(blogId);
        return ResultJson.success();
    }

    @GetMapping("/timeline")
    @ApiOperation("博客时间轴")
    public ResultJson timeline() {
        return apiService.getTimeline(LoginUtil.getVipId());
    }

    @GetMapping("/timeline/{year}/{page}")
    @ApiOperation("博客时间轴数据分页")
    public ResultJson timelineByYear(@PathVariable String year, @PathVariable int page) {
        return apiService.getBlogPageByYear(page, year);
    }

}
